from rest_framework import serializers
from rest_framework.response import Response
from rest_framework.serializers import ModelSerializer
from apps.headlines.models import Channel, Article, Comment
from rest_framework.pagination import PageNumberPagination
from apps.questions.models import Label

# ---------------自定义分页器----------------------
from apps.users.models import User


class PagNun(PageNumberPagination):
    page_size = 6
    page_size_query_param = 'pagesize'
    max_page_size = 6

    # def get_paginated_response(self, data):
    #   return Response({
    #     'count': self.page.paginator.count,  # 总数量
    #     'lists': data,  # 用户数据
    #     'page': self.page.number,  # 当前页数
    #     'pages': self.page.paginator.num_pages,  # 总页数
    #     'pagesize': self.page_size  # 后端指定的页容量
    #   })


# 频道组序列化器
class ChannelsGroupSerializer(serializers.ModelSerializer):
    class Meta:
        model = Channel
        fields = ('id', 'name')


class ArticleSerializerSimple(serializers.ModelSerializer):
    class Meta:
        model = Article
        fields = ("id", "title")


# 用户序列化器
class UserInfoSerializer(serializers.ModelSerializer):
    articles = ArticleSerializerSimple(read_only=True, many=True)
    fans = serializers.PrimaryKeyRelatedField(read_only=True,many=True)
    class Meta:
        model = User
        fields = ('id', 'username', 'avatar', 'articles','fans')


# 文章序列化器
class ChannelInfoSerializer(serializers.ModelSerializer):
    user = UserInfoSerializer(read_only=True)
    collected = serializers.BooleanField(default=False)

    class Meta:
        model = Article
        fields = ("id", "title", "content", "createtime", "user", "collected_users", "collected", "image", "visits")


# 标签序列化器
class LabelsInfoSerializer(serializers.ModelSerializer):
    class Meta:
        model = Label
        fields = ('id', 'label_name')


# 新建文章序列化器
class AddArticleSerializer(ModelSerializer):
    image = serializers.CharField(required=False, default='', allow_blank=True)

    class Meta:
        model = Article
        exclude = ('collected_users',)

    def create(self, validated_data):
        article = super().create(validated_data)
        return Response({'success': True, 'message': '发表成功', 'articleid': article.id})


class ArticleSerializer(ModelSerializer):
    user = serializers.CharField(read_only=True)

    class Meta:
        model = Article
        fields = '__all__'


# -------------------评论文章序列化器-----------------


class SubsPublishCommentSerializer(serializers.ModelSerializer):
    user = UserInfoSerializer(read_only=True)
    subs = serializers.StringRelatedField(many=True)
    parent = serializers.PrimaryKeyRelatedField(read_only=True)
    article = serializers.PrimaryKeyRelatedField(read_only=True)

    class Meta:
        model = Comment
        # fields = ['id','user','subs','createtime']
        fields = '__all__'


class PublishCommentSerializer(serializers.ModelSerializer):
    user = UserInfoSerializer(read_only=True)
    subs = SubsPublishCommentSerializer(many=True)
    parent = serializers.PrimaryKeyRelatedField(read_only=True)
    article = serializers.PrimaryKeyRelatedField(read_only=True)

    class Meta:
        model = Comment
        # fields = ['id','user','subs','createtime']
        fields = '__all__'


# -------------------文章详情序列化器---------------

class ArticleInfoSerializer(serializers.ModelSerializer):
    user = UserInfoSerializer(read_only=True)
    comments = PublishCommentSerializer(read_only=True, many=True)
    labels = serializers.PrimaryKeyRelatedField(read_only=True,many=True)
    collected = serializers.BooleanField(default=False)
    collected_users = serializers.StringRelatedField(read_only=True,many=True)
    class Meta:
        model = Article
        fields = '__all__'


# # --------------------关注序列化器-----------------------------
# class UserLikeSerializer(serializers.ModelSerializer):
#     fans = serializers.StringRelatedField(many=True)
#     class Meta:
#         model = User
#         fields = '__all__'
